﻿using System.Linq;
using Kratos.Framework.ContentManagement.Models.SystemRights;

namespace Kratos.Framework.Modules.Core.Repositories
{
    public static class RepositoryExtensions
    {
        public static bool IsExistUser(this IQueryable<UserInfo> users, string userId)
        {
            return users.GetUser(userId) != null;
        }

        public static UserInfo GetUser(this IQueryable<UserInfo> users, string userId)
        {
            return users.SingleOrDefault(
                u => u.UserId == userId
                );
        }

        /// <summary>
        /// 系统用户与组用户的差集
        /// </summary>
        public static IQueryable<UserInfo> GetUserWithoutGroup(this IQueryable<UserInfo> users, Group group)
        {
            return users.Where(u => !u.Groups.Contains(group));
        }

        public static bool IsExistActionPermission(this IQueryable<ActionPermission> actionPermissions, ActionPermission model)
        {
            return actionPermissions.FirstOrDefault(a => a.ActionName == model.ActionName
                && a.ControllerName == model.ControllerName && a.ActionDescription == model.ActionDescription
                && a.ControllerDescription == model.ControllerDescription) != null;
        }
    }
}
